/*
 * @Author: 哈兰德
 * @Description: 服务端的状态store
 */
const state =() => ({
  userNo:'',
  userAgent:'',
  activityCode:'',
  shiroSid:'',
  query:{

  },
  versionRule:{
    iosApp:'',
    iosOs:'',
    androidApp:'',
    androidOs:''
  }
}) 

const getters = {
  serverUa:state => {

    const isWeixin = /MicroMessenger/i.test(state.userAgent)
    const isAndroid = /Android/i.test(state.userAgent)
    const isIphone = /iPhone/i.test(state.userAgent)
    const isJeTiao = /qfinan/i.test(state.userAgent)

    return {
      isWeixin,
      isAndroid,
      isIphone,
      isJeTiao
    }
  },
  userNo:state => state.userNo,
  activityCode:state => state.activityCode,
  shiroSid:state => state.shiroSid,
  query:state => state.query,
  versionRule:state => state.versionRule
}

const mutations = {
  setUserNo (state, value) {
    console.log(`userNo=${value}`)
    state.userNo=value
  },
  setQuery (state, value) {
    state.query=value
    state.activityCode= value.activityCode
    if (value.userNo){
      state.userNo=value.userNo
    }

    if (value.deviceOs==='ANDROID'){
      state.versionRule.androidOs=value.deviceOsVersion
      state.versionRule.androidApp=value.appVersion
    } else {
      state.versionRule.iosApp=value.appVersion
      if (value.deviceOsVersion){
        let versionStr = decodeURI(value.deviceOsVersion)
        let arr = versionStr.split('+')
        state.versionRule.iosOs=(arr && arr.length>1)?arr[1]:arr[0]
      }
    }
  },
  setVersionRule(state, value){
    state.versionRule=value
  },
  setUserAgent (state, value) {
    state.userAgent = value
  },
  setActivityCode (state, value) {
    state.activityCode=value
  },
  setShiroSid (state, value) {
    state.shiroSid=value
  }
}

const actions = {

}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
